package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.VO.ClassCurriculumVO;
import edu.zqu.instructionalMatters.project.entity.ClassCurriculum;
import edu.zqu.instructionalMatters.project.entity.Semester;
import edu.zqu.instructionalMatters.project.mapper.ClassCurriculumMapper;
import edu.zqu.instructionalMatters.project.mapper.SemesterMapper;
import edu.zqu.instructionalMatters.project.service.CollegeService;
import edu.zqu.instructionalMatters.project.service.CourseService;
import edu.zqu.instructionalMatters.project.service.InstructionClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 班级课程 - 前端控制器
 * @Version 1.0
 */

@RestController
@RequestMapping("/api")
@Api(tags = "授课班级-课程")
public class ClassCurriculumController {

    @Autowired
    private ClassCurriculumMapper classCurriculumMapper;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private SemesterMapper semesterMapper;
    @Autowired
    private CourseService courseService;
    @Autowired
    private InstructionClassService instructionClassService;

    @ApiOperation(value = "获取授课班级-课程列表")
    @RequestMapping(value = "/classCurriculums", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "semesterId", value = "学期id", paramType = "query", required = false),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = false),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = false)
    })
    public ZQUResult get(BigInteger collegeId,
                         BigInteger semesterId,
                         BigInteger courseId,
                         BigInteger instructionClassId) {
        ClassCurriculum classCurriculum1 = new ClassCurriculum().setCollegeId(collegeId)
                .setSemesterId(semesterId).setCourseId(courseId).setInstructionClassId(instructionClassId);
        List<ClassCurriculumVO> list = new ArrayList<>();
        List<ClassCurriculum> list1 = classCurriculumMapper.findAll(classCurriculum1);
        for (ClassCurriculum classCurriculum : list1) {
            ClassCurriculumVO classCurriculumVO = new ClassCurriculumVO();
            BeanUtils.copyProperties(classCurriculum, classCurriculumVO);
            String collegeName = collegeService.getCollegeName(classCurriculum.getCollegeId());           //根据对象的CollegeId查出学校名称
            String courseName = courseService.getCourseName(classCurriculum.getCourseId());               //根据对象的courseId获取课程名称
            String instructionClassName = instructionClassService.getInstructionClassName(classCurriculum.getInstructionClassId());//授课班级名称
            classCurriculumVO.setCollegeName(collegeName);                                                //设置学校名称
            classCurriculumVO.setCourseName(courseName);                                                  //设置课程名称
            classCurriculumVO.setInstructionClassName(instructionClassName);                              //设置授课班级名称
            classCurriculumVO.setSemesterName(null);                                                      //设置学期名称为null
            Semester semester = semesterMapper.selectById(classCurriculum.getSemesterId());               //根据对象的SemesterId查出学期对象
            if (semester != null) {                                                                          //如果不为空
                classCurriculumVO.setSemesterName(semester.getName());                                    //设置学期名称
            }
            list.add(classCurriculumVO);                                                                  //加入list
        }
        return ZQUResult.success(list);                                                                   //成功返回
    }

    @ApiOperation(value = "获取授课班级-课程信息", response = ClassCurriculumVO.class)
    @RequestMapping(value = "/classCurriculum/{id}", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "授课班级-课程id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        ClassCurriculum classCurriculum = classCurriculumMapper.selectById(id);                       //根据id查出classCurriculum对象
        ClassCurriculumVO classCurriculumVO = new ClassCurriculumVO();                                //初始化ClassCurriculumVO
        BeanUtils.copyProperties(classCurriculum, classCurriculumVO);                                  //对象复制赋值
        String collegeName = collegeService.getCollegeName(classCurriculum.getCollegeId());           //根据对象的CollegeId查出学校名称
        String courseName = courseService.getCourseName(classCurriculum.getCourseId());               //根据对象的courseId获取课程名称
        String instructionClassName = instructionClassService.getInstructionClassName(classCurriculum.getInstructionClassId());//授课班级名称
        classCurriculumVO.setCollegeName(collegeName);                                                //设置学校名称
        classCurriculumVO.setCourseName(courseName);                                                  //设置课程名称
        classCurriculumVO.setInstructionClassName(instructionClassName);                              //设置授课班级名称
        classCurriculumVO.setSemesterName(null);                                                      //设置学期名称
        Semester semester = semesterMapper.selectById(classCurriculum.getSemesterId());               //根据对象的SemesterId查出学期对象
        if (semester != null) {
            classCurriculumVO.setSemesterName(semester.getName());                                    //设置学期名称
        }
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", classCurriculumVO);                 //查询成功返回VO对象
    }

    @ApiOperation(value = "添加")
    @PostMapping(value = "/classCurriculum")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "semesterId", value = "学期id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId,
                         BigInteger semesterId,
                         BigInteger courseId,
                         BigInteger instructionClassId,
                         BigInteger byWho) {
        ClassCurriculum classCurriculum = new ClassCurriculum();               //准备数据模型
        classCurriculum.setCollegeId(collegeId);                               //设置学校id
        classCurriculum.setSemesterId(semesterId);                             //设置学期id
        classCurriculum.setCourseId(courseId);                                 //设置课程id
        classCurriculum.setInstructionClassId(instructionClassId);             //授课班级id
        classCurriculum.setByWho(byWho);                                       //设置操作者
        classCurriculumMapper.insert(classCurriculum);                         //执行插入
        return ZQUResult.success();                                            //操作成功返回
    }

    @ApiOperation(value = "更新")
    @PutMapping(value = "/classCurriculum/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "学期id", paramType = "path", required = true),
            @ApiImplicitParam(name = "semesterId", value = "学期id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId,
                            @PathVariable BigInteger id,
                            BigInteger semesterId,
                            BigInteger courseId,
                            BigInteger instructionClassId,
                            BigInteger byWho) {
        ClassCurriculum classCurriculum = classCurriculumMapper.selectById(id);//先查出旧记录
        classCurriculum.setByWho(byWho);                                       //设置操作者
        classCurriculumMapper.updateById(classCurriculum);                     //更新旧记录
        classCurriculumMapper.deleteById(id);                                  //逻辑删除旧记录
        classCurriculum = new ClassCurriculum();                               //准备新建数据模型
        classCurriculum.setCollegeId(collegeId);                               //设置学校id
        classCurriculum.setSemesterId(semesterId);                             //设置学期id
        classCurriculum.setCourseId(courseId);                                 //设置课程id
        classCurriculum.setInstructionClassId(instructionClassId);             //授课班级id
        classCurriculum.setByWho(byWho);                                       //设置操作者
        classCurriculum.setLastVersion(id);                                    //设置上一个版本号
        classCurriculumMapper.insert(classCurriculum);                         //执行插入
        return ZQUResult.success();                                            //操作成功返回
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/classCurriculum/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学期id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        ClassCurriculum classCurriculum = classCurriculumMapper.selectById(id);//先查出旧记录
        classCurriculum.setByWho(byWho);                                       //设置操作者
        classCurriculumMapper.updateById(classCurriculum);                     //更新旧记录
        classCurriculumMapper.deleteById(id);                                  //逻辑删除旧记录
        return ZQUResult.success();                                            //操作成功返回
    }
}

